# CSS Positioning

Positioning can be tricky sometimes!

## Decide which element to display in front!

## Elements can overlap!

## Positioning

The CSS positioning properties allow you to position an element. It can also place an element behind another, and specify what should happen when an element's content is too big.

Elements can be positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the positioning method.

There are four different positioning methods.

## Static Positioning

HTML elements are positioned static by default. A static positioned element is always positioned according to the normal flow of the page.

Static positioned elements are not affected by the top, bottom, left, and right properties.

## Fixed Positioning

An element with a fixed position is positioned relative to the browser window, and will not move even if the window is scrolled:

## Example

p.pos_fixed {
position: fixed;
top: 30px;
right: 5px;
}
Try it yourself »

| Note: IE7 and IE8 support the fixed value only if a !DOCTYPE is specified.

Fixed positioned elements are removed from the normal flow. The document and other elements behave like the fixed positioned element does not exist.

Fixed positioned elements can overlap other elements.

## Relative Positioning

A relative positioned element is positioned relative to its normal position:

## Example

h2.pos_left {
position: relative;
left: -20px;
}
h2.pos_right {
position: relative;
left: 20px;
}
Try it yourself »

The content of relatively positioned elements can be moved and overlap other elements, but the reserved space for the element is still preserved in the normal flow.

## Example

h2.pos_top {
position: relative;
top: -50px;
}
Try it yourself »

Relatively positioned elements are often used as container blocks for absolutely positioned elements.

## Absolute Positioning

An absolute position element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block is <html>:

## Example

h2 {
position: absolute;
left: 100px;
top: 150px;
}
Try it yourself »

Absolutely positioned elements are removed from the normal flow. The document and other elements behave like the absolutely positioned element does not exist.

Absolutely positioned elements can overlap other elements.

## Overlapping Elements

When elements are positioned outside the normal flow, they can overlap other elements.

The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).

An element can have a positive or negative stack order:

## Example

img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
Try it yourself »

An element with greater stack order is always in front of an element with a lower stack order.

| Note: If two positioned elements overlap without a z-index specified, the element positioned last in the HTML code will be shown on top.

## More Examples

Set the shape of an element
This example demonstrates how to set the shape of an element. The element is clipped into this shape, and displayed.

How to show overflow in an element using scroll
This example demonstrates how to set the overflow property to create a scroll bar when an element's content is too big to fit in a specified area.

How to set the browser to automatically handle overflow
This example demonstrates how to set the browser to automatically handle overflow.

Change the cursor
This example demonstrates how to change the cursor.

## All CSS Positioning Properties

Property | Description | Values
--- | --- | ---
bottom | Sets the bottom margin edge for a positioned box | auto length % inherit
clip | Clips an absolutely positioned element | shape auto inherit

cursor

Specifies the type of cursor to be displayed

url
auto
crosshair
default
pointer
move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help

left | Sets the left margin edge for a positioned box | auto length % inherit
overflow | Specifies what happens if content overflows an element's box | auto hidden scroll visible inherit
position | Specifies the type of positioning for an element | absolute fixed relative static inherit
right | Sets the right margin edge for a positioned box | auto length % inherit
top | Sets the top margin edge for a positioned box | auto length % inherit
z-index | Sets the stack order of an element | number auto inherit

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples